#include <HttpRequest.h>
#include <cstddef>
#include <utility>

namespace muduo{
    HttpRequest::HttpRequest():_version("HTTP/1.1"){}
    void HttpRequest::Reset(){
        _method.clear();
        _path.clear();
        _version.clear();
        _body.clear();
        std::smatch matches;
        _matches.swap(matches);
        _header.clear();
        _params.clear();
    }
    void HttpRequest::SetHeader(const std::string& key, const std::string& val){
        _header.insert(std::make_pair(key, val));
    }
    std::string HttpRequest::GetHeader(const std::string& key) const{
        auto iter = _header.find(key);
        if(iter == _header.end()){
            return "";
        }
        return iter->second;
    }
    bool HttpRequest::HasHeader(const std::string& key) const{
        auto iter = _header.find(key);
        if(iter == _header.end()){
            return false;
        }
        return true;
    }
    void HttpRequest::SetParam(const std::string& key, const std::string& val){
        _header.insert(std::make_pair(key, val));
    }
    std::string HttpRequest::GetParam(const std::string& key){
        auto iter = _header.find(key);
        if(iter == _header.end()){
            return "";
        }
        return iter->second;
    }
    bool HttpRequest::HasParam(const std::string& key){
        auto iter = _header.find(key);
        if(iter == _header.end()){
            return false;
        }
        return true;
    }
    size_t HttpRequest::ContentLength(){
        bool ret = HasHeader("Content-Length");
        if(ret == false){
            return 0;
        }
        std::string str = GetHeader("Content-Length");
        return std::stol(str);
    }
    bool HttpRequest::Close() const{
        if(HasHeader("Connection") && GetHeader("Connection") == "keep-alive"){
            return false;
        }
        return true;
    }
}